<?php
/**
 * Created by PhpStorm.
 * User: wangwenfan
 * Date: 2018/7/27
 * Time: 14:26.
 */

namespace App\Service;

use App\Models\Base\Charge;
use App\Models\Login\Account;
use App\Models\LoginLog\LogAccount;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Log;

class ChannelService
{
    /**
     * 获取所有渠道号.
     *
     * @return mixed
     */
    public static function getChannels()
    {
        return Charge::where('pid', '>', 0)->pluck('pid', 'channel_no');
    }

    /**
     * 获取渠道和平台号集合.
     *
     * @return array
     */
    public static function getChannelIds()
    {
        $data = [];
        $charges = Charge::where('pid', '>', 0)->get();
        foreach ($charges as $channel) {
            $data[] = ['channel_no' => $channel->channel_no, 'pid' => $channel->pname->channel_no];
        }

        return $data;
    }

    /**
     * 渠道注册用户.
     *
     * @param string $start_time
     * @param string $end_time
     * @param int    $channelId
     *
     * @return Collection|null
     */
    public static function registerUsers($start_time, $end_time, $channelId)
    {
        return Account::whereBetween('ac_createTime', [$start_time, $end_time])
            ->where('channelId', $channelId)->get();
    }

    /**
     * 渠道登录用户.
     *
     * @param string $start_time
     * @param string $end_time
     * @param int    $channelId
     *
     * @return int
     */
    public static function LoginUsers($start_time, $end_time, $channelId)
    {
        return LogAccount::whereBetween('time', [$start_time, $end_time])
            ->where('channelId', $channelId)
            ->groupBy('userid')->get()->count();
    }

    /**
     * 获取渠道名和编号的集合.
     *
     * @return mixed
     */
    public static function getChannelNameNo()
    {
        return Charge::where('pid', '>', 0)->get()->pluck('name', 'channel_no');
    }

    /**
     * select渠道列表.
     *
     * @return array
     */
    public static function getChannelList()
    {
        $charge = Charge::all()->where('pid', '>', 0)->pluck('name', 'channel_no')->all();
        $data = ['-1' => '全渠道'];
        if ($charge) {
            foreach ($charge as $key => $val) {
                $data[$key] = $val;
            }
        }

        return $data;
    }

    /**
     * select平台列表.
     *
     * @return array
     */
    public static function getPlatformList()
    {
        $platforms = Charge::all()->where('pid', 0)->pluck('name', 'channel_no')->all();
        $data = ['-1' => '全平台'];
        if ($platforms) {
            foreach ($platforms as $key => $val) {
                $data[$key] = $val;
            }
        }

        return $data;
    }

    /**
     * 根据渠道id获取key.
     *
     * @param $channlId
     *
     * @return bool
     */
    public static function getChannelKey($channlId,$app_key='')
    {
        if (empty($app_key)) {
            $charge = Charge::where('channel_no', $channlId)->first();
            if (null == $charge) {
                Log::error('没有这个渠道:'.$channlId);

                return false;
            }
            if (!empty($charge->app_key)) {
                $app_key = $charge->app_key;
            } else {
                $parentCharge = Charge::find($charge->pid);
                $app_key = $parentCharge->app_key;
            }

            return $app_key;
        }

        return false;
    }

    /**
     * 根据渠道id获取平台id.
     *
     * @param $channel_id
     *
     * @return int
     */
    public static function getPlatformId($channel_id)
    {
        $channel = Charge::where('channel_no', $channel_id)->first();
        if (null == $channel) {
            return 0;
        }

        return $channel->pid;
    }
}
